package leetcode;

/**
 * Created by Ghost on 2014/11/26 0026.
 */
public class Symmetric_Tree {
    private void mirrorSelf(TreeNode root){
        if(root==null)
            return;
        TreeNode left = root.left;
        root.left = root.right;
        root.right = left;
        mirrorSelf(root.left);
        mirrorSelf(root.right);
    }
    private void copy(TreeNode newRoot, TreeNode src){
        if(src!=null){
            TreeNode leftSrc = src.left;
            TreeNode rightSrc = src.right;
            if(leftSrc!=null){
                newRoot.left = new TreeNode(leftSrc.val);
                copy(newRoot.left,src.left);
            }
            if(rightSrc!=null){
                newRoot.right = new TreeNode(rightSrc.val);
                copy(newRoot.right, src.right);
            }
        }
    }
    private boolean equal(TreeNode src, TreeNode src2){
        if(src==null && src2==null)
            return true;
        if(src!=null && src2!=null){
            if(src.val==src2.val){
                return equal(src.left,src2.left) && equal(src.right, src2.right);
            }
        }
        return false;
    }
    public boolean isSymmetric(TreeNode root) {
        if(root==null)
            return true;
        TreeNode src = new TreeNode(root.val);
        copy(src,root);
        mirrorSelf(root);
        return equal(src,root);
    }

    public static void main(String[] args){
        Symmetric_Tree symmetric_tree = new Symmetric_Tree();
        TreeNode root = new TreeNode(1);
        /*root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.right.right = new TreeNode(3);*/

        System.out.println(symmetric_tree.isSymmetric(null));
    }
}
